package org.zfes.snowier.cloud.zbss.pmp.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.zfes.snowier.core.data.DataSet;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.zfes.snowier.core.util.ZBeanUtil;
import org.zfes.snowier.core.util.ZDateUtil;
import org.zfes.snowier.core.util.ZObjectUtil;
import org.zfes.snowier.core.util.ZAlert;
import org.zfes.snowier.core.util.ZAssert;
import org.zfes.snowier.common.dao.params.ParamMap;
import org.zfes.snowier.cloud.zbss.pmp.service.IPmpBugsRecordService;
import org.zfes.snowier.cloud.zbss.pmp.service.IPmpBugsService;
import org.zfes.snowier.cloud.zbss.pmp.service.IPmpCloudMemberService;
import org.zfes.snowier.cloud.zbss.pmp.service.IPmpDeduceLineService;
import org.zfes.snowier.cloud.zbss.pmp.service.IPmpDifficultyScoreService;
import org.zfes.snowier.cloud.zbss.pmp.service.IPmpProjectService;
import org.zfes.snowier.cloud.zbss.pmp.service.IPmpProjectVersionService;
import org.zfes.snowier.cloud.zbss.pmp.service.IPmpRequiresService;
import org.zfes.snowier.cloud.zbss.pmp.service.IPmpTasksService;
import org.zfes.snowier.cloud.zbss.pmp.dao.PmpBugsMapper;
import org.zfes.snowier.cloud.zbss.pmp.dto.PmpBugsAssignDto;
import org.zfes.snowier.cloud.zbss.pmp.dto.PmpBugsCreateDto;
import org.zfes.snowier.cloud.zbss.pmp.dto.PmpBugsPmUpdateDto;
import org.zfes.snowier.cloud.zbss.pmp.dto.PmpBugsSubmitDto;
import org.zfes.snowier.cloud.zbss.pmp.dto.PmpBugsTesterUpdateDto;
import org.zfes.snowier.cloud.zbss.pmp.model.PmpBugs;
import org.zfes.snowier.cloud.zbss.pmp.model.PmpBugsRecord;
import org.zfes.snowier.cloud.zbss.pmp.model.PmpCloudMember;
import org.zfes.snowier.cloud.zbss.pmp.model.PmpDeduceLine;
import org.zfes.snowier.cloud.zbss.pmp.model.PmpDifficultyScore;
import org.zfes.snowier.cloud.zbss.pmp.model.PmpProject;
import org.zfes.snowier.cloud.zbss.pmp.model.PmpProjectVersion;
import org.zfes.snowier.cloud.zbss.pmp.model.PmpRequires;
import org.zfes.snowier.cloud.zbss.pmp.model.PmpTasks;

import java.util.stream.Stream;

@Service
public class PmpBugsServiceImpl implements IPmpBugsService {
	@Autowired
	private PmpBugsMapper pmpBugsMapper;
	@Autowired
	private IPmpProjectService pmpProjectService;

	@Autowired
	private IPmpProjectVersionService pmpProjectVersionService;

	@Autowired
	private IPmpTasksService pmpTasksService;
	@Autowired
	private IPmpRequiresService pmpRequiresService;
	@Autowired
	private IPmpCloudMemberService pmpCloudMemberService;
	@Autowired
	private IPmpDeduceLineService deduceLineService;
	@Autowired
	private IPmpDifficultyScoreService difficultyScoreService;

	@Autowired
	private IPmpBugsRecordService bugsRecordService;
	@Autowired
	private IPmpDifficultyScoreService pmpDifficultyScoreService;
	
	@Transactional(rollbackFor = Exception.class)
	@Override
	public void createPmpBugs(Long createUserId, PmpBugsCreateDto createDto) {
		ZBeanUtil.validateBean(createDto);
		// bug来源_D#1任务,2需求,3bug,4其他#
		ZAssert.isTrue(Stream.of(new Byte[] { 1, 2, 3, 4 }).anyMatch(val -> val.equals(createDto.getBugFrom())),"bug来源参数错误");

		// 严重程度#1轻微,2小调整,3中等,4严重交,5非常严重,6宕机#
		ZAssert.isTrue(Stream.of(new Byte[] { 1, 2, 3, 4, 5, 6 }).anyMatch(val -> val.equals(createDto.getSeverity())),"bu严重程度参数错误");

		// 优先级#1低，2中等，3急，4加急#
		ZAssert.isTrue(Stream.of(new Byte[] { 1, 2, 3, 4 }).anyMatch(val -> val.equals(createDto.getPriority())),"bug优先级参数错误");
		
		
		PmpBugs pmpBugs = new PmpBugs();
		ZBeanUtil.setCreateInfo(pmpBugs);

		ZBeanUtil.copy(createDto, pmpBugs, true);
		
		// 测试人员状态
		PmpCloudMember creator = pmpCloudMemberService.loadPmpCloudMemberById(createUserId) .orElseThrow(() -> ZAlert.newSLE("未查到测试人员数据"));
		ZAssert.equals(creator.getStatus(), Byte.valueOf("1"), "测试人员用户状态错误");
		pmpBugs.setCreatorUserId(createUserId).setCreatorUserName(creator.getUserName());

		PmpProject pmpProject = pmpProjectService.loadPmpProjectById(createDto.getProjectId()) .orElseThrow(() -> ZAlert.newSLE("未查询到项目信息"));
		ZAssert.equals(pmpProject.getStatus(), Byte.valueOf("1"), "项目已经删除或者结束");
		
		if (ZObjectUtil.isNotNull(createDto.getProjectVersionId())) {
			PmpProjectVersion version = pmpProjectVersionService.loadPmpProjectVersionById(createDto.getProjectVersionId()).orElseThrow(() -> ZAlert.newSLE("选择版本信息错误"));
			pmpBugs.setProjectVersionId(version.getId()).setProjectVersionName(version.getVersion());
		} else {
			pmpBugs.setProjectVersionId(pmpProject.getVersionId()).setProjectVersionName(pmpProject.getVersionName());
		}
		
		
		if(ZObjectUtil.isNotNull(createDto.getChargeUserId())) {
			PmpCloudMember _chargeUser = pmpCloudMemberService.loadPmpCloudMemberById(createDto.getChargeUserId()).orElseThrow(() -> ZAlert.newSLE("未查找到任务责任人数据"));
			ZAssert.equals(_chargeUser.getStatus(), Byte.valueOf("1"), "责任人用户状态错误");
			pmpBugs.setChargeUserId(_chargeUser.getId()).setChargeUserName(_chargeUser.getUserName());
		}
		
		
		if(ZObjectUtil.isNotNull(createDto.getAssignUserId())) {
			PmpCloudMember _assignUser = pmpCloudMemberService.loadPmpCloudMemberById(createDto.getAssignUserId()).orElseThrow(() -> ZAlert.newSLE("未查找到任务负责人数据"));
			ZAssert.equals(_assignUser.getStatus(), Byte.valueOf("1"), "负责人用户状态错误");
			pmpBugs.setAssignUserName(_assignUser.getUserName()).setAssignUserId(_assignUser.getId());
		}
	
		
		
		if (Byte.valueOf("1").equals(createDto.getBugFrom())) {
			ZAssert.notNull(createDto.getFromTaskId(), "请选择bug来源的任务");
			PmpTasks pmpTasks = pmpTasksService.loadPmpTasksById(createDto.getFromTaskId()).orElseThrow(() -> ZAlert.newSLE("未查找到该任务数据"));
			// 来源任务状态1未指派,2未开始,3进行中,4已提交,5被驳回,6已完成,-1已删除#
			ZAssert.equals(pmpTasks.getStatus(), Byte.valueOf("6"), "任务状态不是已完成，不能提交bug");

			PmpCloudMember chargeUser = pmpCloudMemberService.loadPmpCloudMemberById(pmpTasks.getChargeUserId()).orElseThrow(() -> ZAlert.newSLE("未查找到任务负责人数据"));
			ZAssert.equals(chargeUser.getStatus(), Byte.valueOf("1"), "负责人用户状态错误");

			pmpBugs.setChargeUserId(pmpTasks.getChargeUserId()).setChargeUserName(pmpTasks.getChargeUserName()).setStatus(Byte.valueOf("1"));// 已指派#1未指派,2未开始,3进行中,4修改并提交,5测试验收,6测试驳回,7经理驳回,8已完成,-1已删

		}
		if (Byte.valueOf("2").equals(createDto.getBugFrom())) {
			ZAssert.notNull(createDto.getFromRequireId(), "请选择bug来源的需求");
			PmpRequires requires = pmpRequiresService.loadPmpRequiresById(createDto.getFromRequireId()).orElseThrow(() -> ZAlert.newSLE("未查找到该需求"));
			ZAssert.equals(requires.getStatus(), Byte.valueOf("1"), "需求状态错误");
			ZAssert.equals(requires.getReType(), Byte.valueOf("1"), "此非需求");
			pmpBugs.setFromRequireId(createDto.getFromRequireId()).setFromRequireModuleId(requires.getParentId());
		}
		if (Byte.valueOf("3").equals(createDto.getBugFrom())) {
			ZAssert.notNull(createDto.getFromBugId(), "请选择bug来源的bug");

			PmpBugs fromBug = loadPmpBugsById(createDto.getFromBugId()).orElseThrow(() -> ZAlert.newSLE("未查找到该bug来源"));
			ZAssert.equals(fromBug.getStatus(), Byte.valueOf("8"), "来源bug状态错误");

			pmpBugs.setFromBugId(createDto.getFromBugId());
			pmpBugs.setChargeUserId(fromBug.getChargeUserId()).setChargeUserName(fromBug.getChargeUserName());
		}
		
		
		
		PmpDeduceLine deduceLine = deduceLineService.loadPmpDeduceLineById(pmpBugs.getDeduceId()).orElseThrow(() -> ZAlert.newSLE("未找到改扣分标准"));
		pmpBugs.setDeduceId(deduceLine.getId()).setDeduceScore(deduceLine.getDeduceScore());

		
		PmpDifficultyScore difScore = pmpDifficultyScoreService .loadPmpDifficultyScoreById(createDto.getDifficultyScoreId()) .orElseThrow(() -> ZAlert.newSLE("难度配置错误"));
		pmpBugs.setDifficultyScore(difScore.getScore());
		pmpBugs.setDifficultyScoreId(difScore.getId());
		pmpBugs.setDifficultyScoreName(difScore.getName());
		
		
		
		pmpBugs.setStatus(Byte.valueOf("1"));// bug状态_D#1未指派,2待解决,3进行中,4修改并提交,5测试验收,6测试驳回,7经理驳回,8已完成,9延期处理,10取消，-1已删除#
		pmpBugs.setExpired(Byte.valueOf("1"));// #1正常,2已超时#
		pmpBugs.setProgress(Byte.valueOf("0"));
		pmpBugs.setCreatetime(ZDateUtil.newDate()).setUpdatetime(ZDateUtil.newDate());
		ZBeanUtil.validateBean(pmpBugs);
		pmpBugsMapper.insert(pmpBugs);
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public void assignPmpBugs(PmpBugsAssignDto assignDto) {
		ZBeanUtil.validateBean(assignDto);
		PmpBugs pmpBugs = loadPmpBugsById(assignDto.getId()).orElseThrow(() -> ZAlert.newSLE("未查到bug数据"));
		if (!Stream.of(new Byte[] { 1, 6, 7, 9 }).anyMatch(val -> val.equals(pmpBugs.getStatus()))) {
			ZAlert.serviceLogicalError("当前状态不允许此操作");// bug状态_D#1未指派,2待解决,3进行中,4修改并提交,5测试验收,6测试驳回,7经理驳回,8已完成,9延期处理,10取消，-1已删除#
		}
		ZBeanUtil.copy(assignDto, pmpBugs, true);

		PmpCloudMember assignUser = pmpCloudMemberService.loadPmpCloudMemberById(assignDto.getAssignUserId()).orElseThrow(() -> ZAlert.newSLE("未查到用户数据"));
		ZAssert.equals(assignUser.getStatus(), Byte.valueOf("1"), "用户状态错误");
		pmpBugs.setAssignUserId(assignUser.getId()).setAssignUserName(assignUser.getUserName());

		if (ZObjectUtil.isNotNull(pmpBugs.getChargeUserId())) {
			PmpCloudMember chargeUser = pmpCloudMemberService.loadPmpCloudMemberById(pmpBugs.getChargeUserId()).orElseThrow(() -> ZAlert.newSLE("未查找到任务负责人数据"));
			ZAssert.equals(chargeUser.getStatus(), Byte.valueOf("1"), "负责人用户状态错误");
			pmpBugs.setChargeUserId(chargeUser.getId()).setChargeUserName(chargeUser.getUserName());
		} else {
			ZAssert.notNull(pmpBugs.getChargeUserId(), "该bug未指定负责人");
		}

		PmpDifficultyScore difScore = difficultyScoreService.loadPmpDifficultyScoreById(pmpBugs.getDifficultyScoreId()).orElseThrow(() -> ZAlert.newSLE("未查到评估难度数据"));
		pmpBugs.setDifficultyScoreId(difScore.getId()).setDifficultyScore(difScore.getScore());

		pmpBugs.setAssignTime(ZDateUtil.newDate());

		ZBeanUtil.setUpdatedInfo(pmpBugs);
		pmpBugsMapper.updateByPrimaryKeySelective(pmpBugs);
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public int updateAssignHours(Long id, Integer assignHours) {
		ZAssert.notNull(id, "请选择Bug");
		ZAssert.bigThanZero(assignHours, "时间错误", "请输入正确的时间格式");
		PmpBugs pmpBugs = loadPmpBugsById(id).orElseThrow(() -> ZAlert.newSLE("未查到bug数据"));
		if (!Stream.of(new Byte[] { 1, 2, 3, 4, 5, 6, 7, 9 }).anyMatch(val -> val.equals(pmpBugs.getStatus()))) {
			ZAlert.serviceLogicalError("当前状态不允许此操作");// bug状态_D#1未指派,2待解决,3进行中,4修改并提交,5测试验收,6测试驳回,7经理驳回,8已完成,9延期处理,10取消，-1已删除#
		}
		return pmpBugsMapper.updateAssignHours(id, assignHours);
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public int updateExpiryTime(Long id, Date expiryTime) {
		ZAssert.notNull(id, "请选择Bug");
		ZAssert.notNull(expiryTime, "任务截止日期不能为空");

		PmpBugs pmpBugs = loadPmpBugsById(id).orElseThrow(() -> ZAlert.newSLE("未查到bug数据"));
		if (!Stream.of(new Byte[] { 1, 2, 3, 4, 5, 6, 7, 9 }).anyMatch(val -> val.equals(pmpBugs.getStatus()))) {
			ZAlert.serviceLogicalError("当前状态不允许此操作");// bug状态_D#1未指派,2待解决,3进行中,4修改并提交,5测试验收,6测试驳回,7经理驳回,8已完成,9延期处理,10取消，-1已删除#
		}
		return pmpBugsMapper.updateExpiryTime(id, expiryTime);

	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public int updateExpired(Long id, Byte expired) {
		ZAssert.notNull(id, "请选择Bug");

		PmpBugs pmpBugs = loadPmpBugsById(id).orElseThrow(() -> ZAlert.newSLE("未查到bug数据"));
		if (!Stream.of(new Byte[] { 1, 2, 3, 4, 5, 6, 7, 9 }).anyMatch(val -> val.equals(pmpBugs.getStatus()))) {
			ZAlert.serviceLogicalError("当前状态不允许此操作");// bug状态_D#1未指派,2待解决,3进行中,4修改并提交,5测试验收,6测试驳回,7经理驳回,8已完成,9延期处理,10取消，-1已删除#
		}
		return pmpBugsMapper.updateExpired(id, expired);
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public int updatePriority(Long id, Byte priority) {
		ZAssert.notNull(id, "请选择Bug");
		// 优先级#1低，2中等，3急，4加急#
		ZAssert.isTrue(Stream.of(new Byte[] { 1, 2, 3, 4 }).anyMatch(val -> val.equals(priority)),
				"bug优先级参数错误");

		PmpBugs pmpBugs = loadPmpBugsById(id).orElseThrow(() -> ZAlert.newSLE("未查到bug数据"));
		if (!Stream.of(new Byte[] { 1, 2, 3, 4, 5, 6, 7, 9, 10 }).anyMatch(val -> val.equals(pmpBugs.getStatus()))) {
			ZAlert.serviceLogicalError("当前状态不允许此操作");// bug状态_D#1未指派,2待解决,3进行中,4修改并提交,5测试验收,6测试驳回,7经理驳回,8已完成,9延期处理,10取消，-1已删除#
		}
		return pmpBugsMapper.updatePriority(id, priority);
	}

	// 严重程度
	@Transactional(rollbackFor = Exception.class)
	@Override
	public int updateSeverity(Long id, Byte severity) {
		ZAssert.notNull(id, "请选择Bug");
		// 严重程度#1轻微,2小调整,3中等,4严重交,5非常严重,6宕机#
		ZAssert.isTrue(Stream.of(new Byte[] { 1, 2, 3, 4, 5, 6 }).anyMatch(val -> val.equals(severity)),
				"bu严重程度参数错误");

		PmpBugs pmpBugs = loadPmpBugsById(id).orElseThrow(() -> ZAlert.newSLE("未查到bug数据"));
		if (!Stream.of(new Byte[] { 1, 2, 3, 4, 5, 6, 7, 9, 10 }).anyMatch(val -> val.equals(pmpBugs.getStatus()))) {
			ZAlert.serviceLogicalError("当前状态不允许此操作");// bug状态_D#1未指派,2待解决,3进行中,4修改并提交,5测试验收,6测试驳回,7经理驳回,8已完成,9延期处理,10取消，-1已删除#
		}
		return pmpBugsMapper.updateSeverity(id, severity);
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public int updateFrequency(Long id, Byte frequency) {
		ZAssert.notNull(id, "请选择Bug");

		if (!Stream.of(new Byte[] { 1, 2 }).anyMatch(val -> val.equals(frequency))) {
			ZAlert.serviceLogicalError("状态参数错误");
		}
		
		PmpBugs pmpBugs = loadPmpBugsById(id).orElseThrow(() -> ZAlert.newSLE("未查到bug数据"));
		if (!Stream.of(new Byte[] { 1, 2, 3, 4, 5, 6, 7, 9, 10 }).anyMatch(val -> val.equals(pmpBugs.getStatus()))) {
			ZAlert.serviceLogicalError("当前状态不允许此操作");// bug状态_D#1未指派,2待解决,3进行中,4修改并提交,5测试验收,6测试驳回,7经理驳回,8已完成,9延期处理,10取消，-1已删除#
		}
		
		return pmpBugsMapper.updateFrequency(id, frequency);
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public void updatePmpBugsForTester(PmpBugsTesterUpdateDto updateDto) {
		ZBeanUtil.validateBean(updateDto);
		PmpBugs pmpBugs = loadPmpBugsById(updateDto.getId()).orElseThrow(() -> ZAlert.newSLE("未查到bug数据"));

		ZAssert.equals(pmpBugs.getStatus(), Byte.valueOf("1"), "只有未指派的bug才能修改");
		ZBeanUtil.copy(updateDto, pmpBugs, true);

		PmpDeduceLine deduceLine = deduceLineService.loadPmpDeduceLineById(pmpBugs.getDeduceId())
				.orElseThrow(() -> ZAlert.newSLE("未找到改扣分标准"));
		pmpBugs.setDeduceId(deduceLine.getId()).setDeduceScore(deduceLine.getDeduceScore());
		pmpBugs.setStatus(Byte.valueOf("1"));// 已指派# bug状态_D#1未指派,2待解决,3进行中,4修改并提交,5测试验收,6测试驳回,7经理驳回,8已完成,9延期处理,10取消，-1已删除#

		pmpBugs.setAssignTime(ZDateUtil.newDate());

		ZBeanUtil.setUpdatedInfo(pmpBugs);
		pmpBugsMapper.updateByPrimaryKeySelective(pmpBugs);
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public void updatePmpBugsForPm(PmpBugsPmUpdateDto updateDto) {
		ZBeanUtil.validateBean(updateDto);
		PmpBugs pmpBugs = loadPmpBugsById(updateDto.getId()).orElseThrow(() -> ZAlert.newSLE("未查到bug数据"));

		ZAssert.equals(pmpBugs.getStatus(), Byte.valueOf("1"), "只有未指派的bug才能修改");
		ZBeanUtil.copy(updateDto, pmpBugs, true);

		PmpDeduceLine deduceLine = deduceLineService.loadPmpDeduceLineById(pmpBugs.getDeduceId())
				.orElseThrow(() -> ZAlert.newSLE("未找到改扣分标准"));
		pmpBugs.setDeduceId(deduceLine.getId()).setDeduceScore(deduceLine.getDeduceScore());
		pmpBugs.setStatus(Byte.valueOf("1"));// bug状态_D#1未指派,2待解决,3进行中,4修改并提交,5测试验收,6测试驳回,7经理驳回,8已完成,9延期处理,10取消，-1已删除#
		
		PmpDifficultyScore difScore = difficultyScoreService.loadPmpDifficultyScoreById(pmpBugs.getDifficultyScoreId())
				.orElseThrow(() -> ZAlert.newSLE("未查到评估难度数据"));
		pmpBugs.setDifficultyScoreId(difScore.getId()).setDifficultyScore(difScore.getScore());

		pmpBugs.setAssignTime(ZDateUtil.newDate());

		ZBeanUtil.setUpdatedInfo(pmpBugs);
		pmpBugsMapper.updateByPrimaryKeySelective(pmpBugs);
	}

	// bug状态_D#1未指派,2待解决,3进行中,4修改并提交,5测试验收,6测试驳回,7经理驳回,8已完成,9延期处理,10取消，-1已删除#
	@Transactional(rollbackFor = Exception.class)
	@Override
	public void submitBugs(Long userId, PmpBugsSubmitDto bugsSubmitDto) {
		ZAssert.notNull(bugsSubmitDto.getId(), "请选择任务");
		ZAssert.bigOrEqThanZero(bugsSubmitDto.getWorkHours(), "请输入投入工时", "投入工时参数错误");
		ZAssert.bigOrEqThanZero(bugsSubmitDto.getProgress(), "请提交进度", "进参数错误");

		PmpBugs pmpBugs = loadPmpBugsById(bugsSubmitDto.getId()).orElseThrow(() -> ZAlert.newSLE("未查到bug数据"));
		ZAssert.isTrue(Stream.of(new Byte[] { 2, 3, 6, 7 }).anyMatch(val -> val.equals(pmpBugs.getStatus())),
				"bug当前状态不允许提交");

		ZAssert.equals(userId, pmpBugs.getAssignUserId(), "该bug未指派给当前用户");

		pmpBugs.setWorkHours(bugsSubmitDto.getWorkHours()).setProgress(bugsSubmitDto.getProgress()).setModifyTime(ZDateUtil.newDate());

		// bug状态_D#1未指派,2待解决,3进行中,4修改并提交,5测试验收,6测试驳回,7经理驳回,8已完成,9延期处理,10取消，-1已删除#

		if (ZObjectUtil.isNotNull(bugsSubmitDto.getIsSumitBug())&& Byte.valueOf("1").equals(bugsSubmitDto.getIsSumitBug())) {
			pmpBugs.setStatus(Byte.valueOf("4"));
			ZAssert.equals(bugsSubmitDto.getProgress(), Byte.valueOf("100"), "完成进度不是100%");

		}
		ZBeanUtil.setUpdatedInfo(pmpBugs);
		pmpBugsMapper.updateByPrimaryKeySelective(pmpBugs);

		PmpBugsRecord bugsRecord = new PmpBugsRecord().setBugId(pmpBugs.getId()).setBugName(pmpBugs.getBugName())
				.setChargeUserId(pmpBugs.getChargeUserId()).setChargeUserName(pmpBugs.getChargeUserName())
				.setCreatetime(ZDateUtil.newDate()).setProgress(bugsSubmitDto.getProgress())
				.setWorkHours(bugsSubmitDto.getWorkHours()).setRemark(bugsSubmitDto.getRemark());
		bugsRecordService.createPmpBugsRecord(bugsRecord);

	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public void testerCheckBugs(List<Long> ids, Byte accept) {
		ZAssert.notEmpty(ids, "请选择数据");
		ZAssert.notNull(accept, "参数错误");
		ids.forEach(id -> {
			//bug状态_D#1未指派,2待解决,3进行中,4修改并提交,5测试验收,6测试驳回,7经理驳回,8已完成,9延期处理,10取消，-1已删除#
			PmpBugs pmpBugs = loadPmpBugsById(id).orElseThrow(() -> ZAlert.newSLE("未查到bug数据"));
			ZAssert.equals(pmpBugs.getStatus(), Byte.valueOf("4"), "该bug未提交");
			if (Byte.valueOf("1").equals(accept)) {
				setPmpBugsStatus(id, Byte.valueOf("5"));
			} else {
				setPmpBugsStatus(id, Byte.valueOf("6"));
			}
		});
	}

	// bug状态_D#1未指派,2待解决,3进行中,4修改并提交,5测试验收,6测试驳回,7经理驳回,8已完成,9延期处理,10取消，-1已删除#
	@Transactional(rollbackFor = Exception.class)
	@Override
	public void pmCheckBugs(List<Long> ids, Byte accept) {
		ZAssert.notEmpty(ids, "请选择数据");
		ZAssert.notNull(accept, "参数错误");

		ids.forEach(id -> {
			PmpBugs pmpBugs = loadPmpBugsById(id).orElseThrow(() -> ZAlert.newSLE("未查到bug数据"));
			ZAssert.equals(pmpBugs.getStatus(), Byte.valueOf("5"), "测试人员未验收");
			if (Byte.valueOf("1").equals(accept)) {
				setPmpBugsStatus(id, Byte.valueOf("7"));
			} else {
				setPmpBugsStatus(id, Byte.valueOf("8"));
			}
		});

	}

	@Transactional(rollbackFor = Exception.class, readOnly = true)
	@Override
	public Optional<PmpBugs> loadPmpBugsById(Long id) {
		if (id != null) {
			return Optional.ofNullable(pmpBugsMapper.selectByPrimaryKey(id));
		}
		return Optional.empty();

	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public void bactchSetPmpBugsStatus(List<Long> ids, Byte status) {
		ZAssert.notEmpty(ids, "请选择要操作的数据");
		ZAssert.notNull(status, "状态参数错误");
		if (!Stream.of(new Byte[] { -1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }).anyMatch(val -> val.equals(status))) {
			ZAlert.serviceLogicalError("状态参数错误");
		}
		pmpBugsMapper.batchUpdateStatus(ids, status);
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public void setPmpBugsStatus(Long id, Byte status) {
		ZAssert.notNull(id, "请选择要操作的数据");
		ZAssert.notNull(status, "状态参数错误");
		if (!Stream.of(new Byte[] { -1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }).anyMatch(val -> val.equals(status))) {
			ZAlert.serviceLogicalError("状态参数错误");
		}
		PmpBugs pmpBugs = loadPmpBugsById(id).orElseThrow(() -> ZAlert.newSLE("未查到bug数据"));
		pmpBugs.setUpdatetime(ZDateUtil.newDate());
		pmpBugs.setStatus(status);
		pmpBugsMapper.updateByPrimaryKeySelective(pmpBugs);
	}

	@Transactional(rollbackFor = Exception.class, readOnly = true)
	@Override
	public DataSet loadPmpBugsDataSet(Map<String, Object> params) {
		ParamMap pm = ParamMap.filterParam(params);
		DataSet ds = DataSet.newDS2(pmpBugsMapper.selectListCount(pm), pmpBugsMapper.selectMapListPage(pm));
		return ds;
	}
	
	// bug状态_D#1未指派,2待解决,3进行中,4修改并提交,5测试验收,6测试驳回,7经理驳回,8已完成,9延期处理,10取消，-1已删除#
	@Transactional(rollbackFor = Exception.class)
	@Override
	public void setPmpBugsStatusByPm(Long id, Byte status) {
		ZAssert.notNull(id, "请选择要操作的数据");
		ZAssert.notNull(status, "状态参数错误");
		if (!Stream.of(new Byte[] { -1, 1, 2, 3, 7, 8, 9, 10 }).anyMatch(val -> val.equals(status))) {
			ZAlert.serviceLogicalError("项目经理不可对bug进行此操作");
		}
		pmpBugsMapper.updateStatus(id, status);
		
	}

}